[C] POSIX Threads
Publicado por Enzo de Brito Ferber 26/01/2009
[ Hits: 6.936 ]
Homepage: http://www.maximasonorizacao.com.br
O foco deste programa é fazer com que o processo filho criado por fork() não seja terminado quando o processo pai termina, e para isso, usei threads. Para terminar o programa, digite no terminal:
kill <child_pid>
O child_pid é mostrado ao usuário quando o programa é executado. Bem legal :)
// pthread2.c /* * Enzo Ferber : <enzo@veloxmail.com.br> * * POSIX Threads Test 2 * @ Exit parent and go on with child. * * $ gcc -lpthread -o pthread2 pthread2.c */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> // getpid #include <pthread.h> // POSIX Threads #include <sys/types.h> // getpid // child in infinite loop void *childfunction ( void *ptr ) { pthread_t self = pthread_self (); pthread_detach ( self ); for ( ; ; ) { printf ( "[*] Here I am again ... \n"); sleep (2); } } // main function int main ( int argc, char *argv[] ) { pthread_t child; int cret; // child pthread_create () return // child if ( !fork() ) { printf ( "[*] Child proccess begining ... \n" ); cret = pthread_create ( &child, NULL, childfunction, NULL ); printf ( "[*] Child PID: %d\n", getpid () ); pthread_join ( child, NULL ); } // parent else { printf ( "[*] Parent PID: %d\n", getpid () ); printf ( "[*] Aborting parent ...\n" ); exit (0); } // end main return 0; } // EoF
Exemplo simples de socket em C/C++
Nenhum comentário foi encontrado.
Como compartilhar a tela do Ubuntu com uma Smart TV (LG, Samsung, etc.)
Descritores de Arquivos e Swappiness
tux-gpt - Assistente de IA para o Terminal
Instalação e configuração do Chrony
Programa IRPF - Guia de Instalação e Resolução de alguns Problemas
Como instalar no Linux Jogos da Steam só para Windows
Instalando o Team Viewer no Debian Trixie - problema no Policykit
O Que Fazer Após Instalar Ubuntu 25.04